package com.example.springboot.config;

import com.example.springboot.config.interceptor.JwtInterceptor;
import com.example.springboot.utils.SystemUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/");
        if(SystemUtil.isMacOs()){
            registry.addResourceHandler("/files/**")
                    .addResourceLocations("file:/Users/cpf/workspace/factory/files/");
            registry.addResourceHandler("/wordToHtml/**")
                    .addResourceLocations("file:/Users/cpf/workspace/factory/springboot/src/main/resources/wordToHtml/");
        }else if(SystemUtil.isWindows()){
            registry.addResourceHandler("/files/**")
                    .addResourceLocations("file:D:/workspace/factory/files/");
            registry.addResourceHandler("/RaceFiles/**")
                    .addResourceLocations("file:D:/workspace/factory/RaceFiles/");
            registry.addResourceHandler("/raceImg/**")
                    .addResourceLocations("file:D:/workspace/factory/raceImg/");
            registry.addResourceHandler("/avatar/**")
                    .addResourceLocations("file:D:/workspace/factory/avatar/");
            registry.addResourceHandler("/templates/**")
                    .addResourceLocations("file:D:/workspace/factory/springboot/src/main/resources/templates/");
            registry.addResourceHandler("/wordToHtml/**").
                    addResourceLocations("file:D:\\workspace\\factory\\springboot\\src\\main\\resources\\wordToHtml\\");
        }else {
            registry.addResourceHandler("/files/**")
                    .addResourceLocations("file:/www/wwwroot/factory-back/files/");
            registry.addResourceHandler("/RaceFiles/**")
                    .addResourceLocations("file:/www/wwwroot/factory-back/RaceFiles/");
            registry.addResourceHandler("/raceImg/**")
                    .addResourceLocations("file:/www/wwwroot/factory-back/raceImg/");
            registry.addResourceHandler("**/wordToHtml/**").
                    addResourceLocations("file:/www/wwwroot/factory-back/wordToHtml");
            registry.addResourceHandler("/avatar/**")
                    .addResourceLocations("file:/www/wwwroot/factory-back/avatar/");
            registry.addResourceHandler("/templates/**")
                    .addResourceLocations("file:/www/wwwroot/factory-back/templates/");
        }

        super.addResourceHandlers(registry);
    }

//    @Override
//    protected void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(jwtInterceptor())
//        registry.addPathPatterns("/**")
//                .excludePathPatterns("/user/login", "/user/register", "/**/export", "/**/import", "/download/**"
//                        ,"/files/**","/avatar/**","/wordToHtml/**","/templates/**")
//                .excludePathPatterns("/swagger**/**", "/webjars/**", "/v3/**", "/doc.html");
//        super.addInterceptors(registry);
//    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOriginPatterns("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true).maxAge(3600);
    }
//    @Bean
//    public JwtInterceptor jwtInterceptor() {
//        return new JwtInterceptor();
//    }

}
